package tue.cse.advanceddatabases.project;

import java.util.List;
import java.util.logging.Logger;

import javax.faces.context.FacesContext;

import org.dom4j.Element;

import tue.cse.advanceddatabases.project.db.User;
import tue.cse.advanceddatabases.project.db.UserManager;
import tue.cse.advanceddatabases.project.db.JPAResourceBean;
import tue.cse.advanceddatabases.project.xml.album.Album;
import tue.cse.advanceddatabases.project.xml.album.AlbumsParser;

public class UserBean {
  	private final Logger logger = Logger.getLogger(UserBean.class.getName());
  	
  	protected JPAResourceBean jpaResourceBean;
  	private User _user = new User();
  	private UserManager _userManager;
	
	private UserManager getUserManager() {
		if (_userManager == null)
			_userManager = new UserManager(jpaResourceBean);
		return _userManager;
	}
	
  	private List<User> users;
  	private String name;
  	private String loginID;
  	private String country;
  	private int numberUsers;
  	
  	/*
	 * public properties for private properties
	 */
  	
  	public void setJpaResourceBean(JPAResourceBean jpaResourceBean) {
          this.jpaResourceBean = jpaResourceBean;
  	}

        public JPAResourceBean getJpaResourceBean() {
            return jpaResourceBean;
        }
        
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getLoginID() {
		return loginID;
	}

	public void setLoginID(String loginID) {
		this.loginID = loginID;
	}
	
	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}
	
	public int getNumberUsers() {
		return numberUsers;
	}
  	
	/** return users for "LoginID" **/
	public List<User> getUsers(){
		
		if (users != null){
			return users;
		}
		//TODO: something that will list 10 users before we search for them
		//else{
		//  	UserManager um = getUserManager();
		//  	return um.getAllUsers();
		//}
		
		return null;		
	}
	
	/*
	 * Actions: public methods (query constructions)
	 */
	
	/** search users by search criteria "UserId", "LoginId" or "Country" properties **/
	public String searchUsers(){
		logger.info(" Search Users by String:" + loginID);
		users = null;
		UserManager um = getUserManager();
		
		users = um.getUsersBySearchCriteria(loginID, name, country);
		
		if (users != null)
		  numberUsers = users.size();
		else
		  numberUsers = 0;

		logger.info("Number of users is " + numberUsers);
		
		return "success";
	}
	
	/** search users by "userKey" parameter **/
	/** return current user searched by id **/
	public User getUser(){
		FacesContext facesContext = FacesContext.getCurrentInstance();
		String userId = (String) facesContext.getExternalContext().getRequestParameterMap().get("userKey");
		
		UserManager um = getUserManager();
		_user = um.findUserByUserID(Long.parseLong(userId));
		
		return _user;		
	}
}
